package com.itcpeng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itcpeng.constant.MessageConstant;
import com.itcpeng.entity.Result;
import com.itcpeng.pojo.Package;
import com.itcpeng.service.PackageService;
import com.itcpeng.util.QiNiuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

@RestController
@RequestMapping(value = "/package")
public class PackageController {

    @Reference
    private PackageService packageService;

    @Autowired
    private JedisPool jedisPool;

    //查询所有套餐
    @GetMapping(value = "/getPackage")
    public Result getPackage() {
        //查询所有套餐列表
        List<Package> list = packageService.findAll();
        //拼接套餐图片的完整路径
        if (list != null) {
            //取出list中每个元素给pkg变量赋值
            list.forEach(pkg -> {
                pkg.setImg("http://" + QiNiuUtil.DOMAIN + "/" + pkg.getImg());
            });
        }

        Jedis jedis = jedisPool.getResource();
        String data = jedis.get("PackagelList");
        if(data == null) {
            String jsonString = JSON.toJSONString(list);
            List<Package> VA = JSON.parseArray(jsonString, Package.class);
            jedis.set("PackagelList",jsonString);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, VA);
        } else {
            //不为空直接从redis中获取数据
            String pkg1 = jedis.get("PackagelList");
            List<Package> jsonString = JSON.parseArray(pkg1,Package.class);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, jsonString);
        }

    }

    //根据套餐id执行多表查询(通过编号查询套餐详情)
    @GetMapping(value = "/findByIds")
    public Result findById(Integer id) {
        //调用业务
        Package pkg = packageService.findByIds(id);
        //拼接完整路径
        pkg.setImg("http://" + QiNiuUtil.DOMAIN + "/" + pkg.getImg());

        //获得redis的数据
       Jedis jedis = jedisPool.getResource();
       //获得数据
       String data = jedis.get("pkg");

       //判断数据是否为空
        if(data == null) {
        //存入redis中
            String jsonString = JSON.toJSONString(pkg);
            jedis.set("pkg",jsonString);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, jsonString);
        } else {
            //不为空直接从redis中获取数据
            String pkg1 = jedis.get("pkg");
            Package jsonString = JSON.parseObject(pkg1,Package.class);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, jsonString);
        }

    }

    // 查询套餐
    @GetMapping(value = "/findPackageById")
    public Result findPackageById(Integer id) {
   //调用业务
        Package pkg = packageService.findPackageById(id);
        //拼接完整路径
        pkg.setImg("http://" + QiNiuUtil.DOMAIN + "/" + pkg.getImg());
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, pkg);
    }
}
